/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Command;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BoxLayout;
import core.IMAPMIDlet;

/**
 *
 * @author Kvest
 */
public class UpdateForm extends Form
{
    public static final int EMPTY_COMMAND_ID = 0;
    public static final int UPDATE_COMMAND_ID = 1;
    
    private String updateLink;
    
    public UpdateForm()
    {
        super("updating");
        
        updateLink = "";
        
        init();
    }
    
    private void init()
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        setScrollable(false);

        TextArea text = new TextArea((new Label("update_text")).getText(), 1, 30);
        text.setEditable(false);
        text.setGrowByContent(true); 
        addComponent(text);
        

        //Команды
        Command emptyCommand = new Command(" ", EMPTY_COMMAND_ID);
        addCommand(emptyCommand);
        
        Command update = new Command("update", UPDATE_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
                update();
                
                evt.consume();
            }
        };
        addCommand(update);
    }
    
    private void update()
    {
        if (!updateLink.equals(""))
        {
            //Инициируем загрузку
            try
            {
                IMAPMIDlet.getMidlet().platformRequest(updateLink);
            }catch(Exception e){}
                    
            //Закрываем приложение
            IMAPMIDlet.getMidlet().destroyApp(true);
        }
    }

    public void setUpdateLink(String updateLink) 
    {
        this.updateLink = updateLink;
    }
}
